package com.itzmn.newcode.offer;

/**
 * @Author: 张梦楠
 * @Date: 2018/11/20 17:02
 * 简书：https://www.jianshu.com/u/d611be10d1a6
 * 码云：https://gitee.com/zhangqiye
 * @Description:  剑指Offer ，将字符串语句反转
 *
 *
 */
public class ReverseString {


    public static void main(String[] args) {

        new ReverseString().init();

    }

    public void init(){

        ReverseSentence(" ");
    }

    public String ReverseSentence(String str) {

        if (str.trim().equals("") || str == null){
            return str;
        }

        String[] split = str.split(" ");

        for (int i=0;i<split.length/2;i++){
            String s = split[i];
            split[i] = split[split.length-1-i];
            split[split.length-1-i] = s;
        }

        StringBuilder stringBuilder = new StringBuilder();
        for (int i=0;i<split.length;i++){

            stringBuilder.append(split[i]+" ");
        }

        String substring = "";
        String s = stringBuilder.toString();

            substring = s.substring(0, s.lastIndexOf(" "));

        System.out.println(substring);

        return substring;

    }

}
